package com.ychs.servlet;

import com.ychs.entity.College;
import com.ychs.entity.Major;
import com.ychs.entity.Member;
import com.ychs.service.IBaseService;
import com.ychs.service.MemberService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/7/21
 */
@WebServlet(value = "/MemberServlet")
public class MemberServlet extends BaseServlet {
    private IBaseService<Member> service = new MemberService();

    @Override
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter add method");
        String name = request.getParameter("name");
        String code = request.getParameter("code");
        String sex = request.getParameter("sex");
        String grade = request.getParameter("grade");

        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String county = request.getParameter("county");
        String nativePlace = province+"-"+city+"-"+county;

        String phone = request.getParameter("phone");

        String collegeId = request.getParameter("college");
        College college = new College();
        college.setId(Integer.parseInt(collegeId));

        String majorId = request.getParameter("major");
        Major major = new Major();
        major.setId(Integer.parseInt(majorId));

        String schoolJob = request.getParameter("schoolJob");
        String joinDate = request.getParameter("joinDate");
        String state = request.getParameter("state");
        String labJob = request.getParameter("labJob");
        String remark = request.getParameter("remark");

        Member member = new Member();
        member.setName(name);
        member.setCode(code);
        member.setSex(Integer.parseInt(sex));
        member.setGrade(Integer.parseInt(grade));
        member.setNativePlace(nativePlace);
        member.setPhone(phone);
        member.setCollege(college);
        member.setMajor(major);
        member.setSchoolJob(schoolJob);
        member.setJoinDate(joinDate);
        member.setState(state);
        member.setLabJob(labJob);
        member.setRemark(remark);

        logger.info(member.toString());

        boolean result = service.add(member);
        logger.info(result);
        String json = json(result);
        response.getWriter().write(json);
    }


    @Override
    protected void modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter modify method");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String code = request.getParameter("code");
        String sex = request.getParameter("sex");
        String grade = request.getParameter("grade");

        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String county = request.getParameter("county");
        String nativePlace = province+"-"+city+"-"+county;

        String phone = request.getParameter("phone");

        String collegeId = request.getParameter("college");
        College college = new College();
        college.setId(Integer.parseInt(collegeId));

        String majorId = request.getParameter("major");
        Major major = new Major();
        major.setId(Integer.parseInt(majorId));

        String schoolJob = request.getParameter("schoolJob");
        String joinDate = request.getParameter("joinDate");
        String state = request.getParameter("state");
        String labJob = request.getParameter("labJob");
        String remark = request.getParameter("remark");

        Member member = new Member();
        member.setId(Integer.parseInt(id));
        member.setName(name);
        member.setCode(code);
        member.setSex(Integer.parseInt(sex));
        member.setGrade(Integer.parseInt(grade));
        member.setNativePlace(nativePlace);
        member.setPhone(phone);
        member.setCollege(college);
        member.setMajor(major);
        member.setSchoolJob(schoolJob);
        member.setJoinDate(joinDate);
        member.setState(state);
        member.setLabJob(labJob);
        member.setRemark(remark);

        logger.info(member.toString());

        boolean result = service.modify(member);
        logger.info(result);
        String json = json(result);
        response.getWriter().write(json);
    }

    @Override
    protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter remove method");
        String id = request.getParameter("id");
        boolean result = service.remove(Integer.parseInt(id));
        logger.info(result);
        String json = json(result);
        response.getWriter().write(json);
    }

    @Override
    protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter search method");
        Map<String, Object> conditions = new HashMap<>();

        String searchName = request.getParameter("searchName");
        String searchCollege = request.getParameter("searchCollege");
        String searchMajor = request.getParameter("searchMajor");
        String searchGrade = request.getParameter("searchGrade");
        String searchPhone = request.getParameter("searchPhone");
        String searchState = request.getParameter("searchState");
        String searchLabJob = request.getParameter("searchLabJob");

        if(searchName!=null&&!"".equals(searchName)){
            conditions.put("name",searchName);
        }
        if(searchCollege!=null&&!"".equals(searchCollege)){
            conditions.put("college",searchCollege);
        }
        if(searchMajor!=null&&!"".equals(searchMajor)){
            conditions.put("major",searchMajor);
        }
        if(searchGrade!=null&&!"".equals(searchGrade)){
            conditions.put("grade",searchGrade);
        }
        if(searchPhone!=null&&!"".equals(searchPhone)){
            conditions.put("phone",searchPhone);
        }
        if(searchState!=null&&!"".equals(searchState)){
            conditions.put("state",searchState);
        }
        if(searchLabJob!=null&&!"".equals(searchLabJob)){
            conditions.put("labJob",searchLabJob);
        }

        int count = service.searchTotalNumber(conditions);

        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        if (page != null && limit != null) {
            int begin = (Integer.parseInt(page) - 1) * Integer.parseInt(limit);
            int pageSize = Integer.parseInt(limit);
            conditions.put("begin", begin);
            conditions.put("pageSize", pageSize);
        }else{
            conditions.put("begin", 0);
            //2是为了方便测试，一般为10
            conditions.put("pageSize", 10);
        }

        List<Member> members = service.search(conditions);
        String json = json(count, members);
        logger.info("json"+json);
        response.getWriter().write(json);
    }

    @Override
    protected void searchById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter searchById method");
        String id = request.getParameter("id");
        Member member = service.searchById(Integer.parseInt(id));
        String json = json(member);
        logger.info("searchById:"+json);
        response.getWriter().write(json);
    }

}
